package com.test.mylotterysystem.comment.utils;

import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class SecurityUtil {
    /**
     * 对密码进行加密
     *
     * @param password
     * @return
     */
    public static String encrypt(String password) {
        String prefix = UUID.randomUUID().toString().replace("-", "");
        String temp = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        return prefix + temp;
    }

    /**
     * 判断密码是否正确
     *
     * @param password
     * @param sqlPassword
     * @return
     */
    public static boolean verify(String password, String sqlPassword) {
        String salt = sqlPassword.substring(0, 32);
        String temp = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        return (salt + temp).equals(sqlPassword);
    }
}
